﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Boomerang.Management.UIForms;
using Boomerang.Common.Crm;
using Boomerang.Common.Enumerations;
using Boomerang.Common.Crm.DiscoveryService;
using Boomerang.Management.UIControlers;
using Boomerang.Common.Sms;

namespace Boomerang.Management.UIControls
{
    public partial class BoomerangLogonControl : UserControl
    {

        public BoomerangLogonControl()
        {
            InitializeComponent();
        }

        public BoomerangLogonControl(SmsConnectionOptions options)
            : this()
        {
            Options = options;
        }


        public SmsConnectionOptions Options
        {
            get
            {
                SmsConnectionOptions options = new SmsConnectionOptions();                
                options.ServerUrl = textBoxServer.Text;
                if (checkBoxDefaultCredentials.Checked)
                {
                    options.UseDefaultCredentials = true;
                }
                else
                {
                    options.UseDefaultCredentials = false;
                    options.Password = textBoxPassword.Text;
                    options.UserName = textBoxUserName.Text;
                    options.Domain = textBoxDomain.Text;
                }

                options.BoomerangUserName = textBoxSmsUserName.Text;
                options.BoomerangPassword = textBoxSmsPassword.Text;

                options.UseHttps = (comboBoxProtocol.SelectedIndex == 1);
                options.LicenseKey = textBoxLicenseKey.Text;

                return options;
            }

            set
            {
                textBoxServer.Text = value.ServerUrl;

                if (value.UseDefaultCredentials)
                {
                    checkBoxDefaultCredentials.Checked = true;
                }
                else
                {
                    checkBoxDefaultCredentials.Checked = false;
                    textBoxPassword.Text = value.Password;
                    textBoxUserName.Text = value.UserName;
                    textBoxDomain.Text = value.Domain;
                }

                textBoxSmsUserName.Text = value.BoomerangUserName;
                textBoxSmsPassword.Text = value.BoomerangPassword;

                textBoxLicenseKey.Text = value.LicenseKey;
                comboBoxProtocol.SelectedIndex = (value.UseHttps) ? 1 : 0;

            }
        }

        private void checkBoxDefaultCredentials_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxCredentials.Enabled = !checkBoxDefaultCredentials.Checked;
        }


    }
}
